/**
 * jQuery EasyUI 1.4
 * 
 * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
 * 
 * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt To use it
 * on other terms please contact us at info@jeasyui.com
 * 
 */
(function($) {
	function createDatetimebox(target) {
		var datetimebox = $.data(target, "datetimebox");
		var opts = datetimebox.options;
		$(target).datebox($.extend({}, opts, {
			onShowPanel : function() {
				var value = $(target).datetimebox("getValue");
				setValue(target, value, true);
				opts.onShowPanel.call(target);
			},
			formatter : $.fn.datebox.defaults.formatter,
			parser : $.fn.datebox.defaults.parser
		}));
		$(target).removeClass("datebox-f").addClass("datetimebox-f");
		$(target).datebox("calendar").calendar({
			onSelect : function(date) {
				var opts = $(this.target).datetimebox('options');
				var hours=$(this.target).datetimebox("spinner").timespinner("getHours");
				var minutes=$(this.target).datetimebox("spinner").timespinner("getMinutes");
				if(!isNaN(hours)){
					date.setHours(hours);
				}
				if(!isNaN(minutes)){
					date.setMinutes(minutes);
				}
				setValue(this.target, opts.formatter.call(this.target, date));
				$(this.target).combo('hidePanel');
				opts.onSelect.call(target, date);
			}
		});
		var panel = $(target).datebox("panel");
		if (!datetimebox.spinner) {
			var p = $("<div style=\"padding:2px\"><input style=\"width:80px\"></div>").insertAfter(panel.children("div.datebox-calendar-inner"));
			datetimebox.spinner = p.children("input");
		}
		datetimebox.spinner.timespinner({
			width : opts.spinnerWidth,
			showSeconds : opts.showSeconds,
			separator : opts.timeSeparator
		}).unbind(".datetimebox").bind("mousedown.datetimebox", function(e) {
			e.stopPropagation();
		});
		setValue(target, opts.value);
	};
	function setCalendar(target) {
		var c = $(target).datetimebox("calendar");
		var t = $(target).datetimebox("spinner");
		var _b = c.calendar("options").current;
		return new Date(_b.getFullYear(), _b.getMonth(), _b.getDate(), t.timespinner("getHours"), t.timespinner("getMinutes"), t.timespinner("getSeconds"));
	};
	function doQuery(_d, q) {
		setValue(_d, q, true);
	};
	function doEnter(target) {
		var opts = $.data(target, "datetimebox").options;
		var _11 = setCalendar(target);
		setValue(target, opts.formatter.call(target, _11));
		$(target).combo("hidePanel");
	};
	function setValue(target, value, remainText) {
		var opts = $.data(target, "datetimebox").options;
		$(target).combo("setValue", value);
		if (!remainText) {
			if (value) {
				var _16 = opts.parser.call(target, value);
				$(target).combo("setValue", opts.formatter.call(target, _16));
				$(target).combo("setText", opts.formatter.call(target, _16));
			} else {
				$(target).combo("setText", value);
			}
		}
		var _16 = opts.parser.call(target, value);
		$(target).datetimebox("calendar").calendar("moveTo", _16);
		$(target).datetimebox("spinner").timespinner("setValue", _17(_16));
		function _17(_18) {
			function _19(_1a) {
				return (_1a < 10 ? "0" : "") + _1a;
			}
			;
			var tt = [ _19(_18.getHours()), _19(_18.getMinutes()) ];
			if (opts.showSeconds) {
				tt.push(_19(_18.getSeconds()));
			}
			return tt.join($(target).datetimebox("spinner").timespinner("options").separator);
		};
	};
	$.fn.datetimebox = function(options, param) {
		if (typeof options == "string") {
			var methods = $.fn.datetimebox.methods[options];
			if (methods) {
				return methods(this, param);
			} else {
				return this.datebox(options, param);
			}
		}
		options = options || {};
		return this.each(function() {
			var datetimebox = $.data(this, "datetimebox");
			if (datetimebox) {
				$.extend(datetimebox.options, options);
			} else {
				$.data(this, "datetimebox", {
					options : $.extend({}, $.fn.datetimebox.defaults,$.fn.datetimebox.parseOptions(this), options)
				});
			}
			createDatetimebox(this);
		});
	};
	$.fn.datetimebox.methods = {
		options : function(jq) {
			var opts = jq.datebox("options");
			return $.extend($.data(jq[0], "datetimebox").options, {
				originalValue : opts.originalValue,
				disabled : opts.disabled,
				readonly : opts.readonly
			});
		},
		spinner : function(jq) {
			return $.data(jq[0], "datetimebox").spinner;
		},
		setValue : function(jq, value) {
			return jq.each(function() {
				setValue(this, value);
			});
		},
		reset : function(jq) {
			return jq.each(function() {
				var opts = $(this).datetimebox("options");
				$(this).datetimebox("setValue", opts.originalValue);
			});
		}
	};
	$.fn.datetimebox.parseOptions = function(target) {
		return $.extend({}, $.fn.datebox.parseOptions(target), $.parser.parseOptions(target, [ "timeSeparator", "spinnerWidth", {
			showSeconds : "boolean"
		} ]));
	};
	$.fn.datetimebox.defaults = $.extend({}, $.fn.datebox.defaults,{
		spinnerWidth : "100%",
		showSeconds : true,
		timeSeparator : ":",
		keyHandler : {
			up : function(e) {
			},
			down : function(e) {
			},
			left : function(e) {
			},
			right : function(e) {
			},
			enter : function(e) {
				doEnter(this);
			},
			query : function(q, e) {
				doQuery(this, q);
			}
		},
		buttons : [ {
			text : function(_23) {
				return $(_23).datetimebox("options").currentText;
			},
			handler : function(_24) {
				$(_24).datetimebox("calendar").calendar({
					year : new Date().getFullYear(),
					month : new Date().getMonth() + 1,
					current : new Date()
				});
				doEnter(_24);
			}
		}, {
			text : function(_25) {
				return $(_25).datetimebox("options").okText;
			},
			handler : function(_26) {
				doEnter(_26);
			}
		}, {
			text : function(_27) {
				return $(_27).datetimebox("options").closeText;
			},
			handler : function(_28) {
				$(this).closest("div.combo-panel").panel("close");
			}
		} ],
		formatter : function(_29) {
			var h = _29.getHours();
			var M = _29.getMinutes();
			var s = _29.getSeconds();
			function _2a(_2b) {
				return (_2b < 10 ? "0" : "") + _2b;
			};
			var _2c = $(this).datetimebox("spinner").timespinner("options").separator;
			var r = $.fn.datebox.defaults.formatter(_29) + " " + _2a(h) + _2c + _2a(M);
			if ($(this).datetimebox("options").showSeconds) {
				r += _2c + _2a(s);
			}
			return r;
		},
		parser : function(s) {
			if ($.trim(s) == "") {
				return new Date();
			}
			var dt = s.split(" ");
			var d = $.fn.datebox.defaults.parser(dt[0]);
			if (dt.length < 2) {
				return d;
			}
			var _2d = $(this).datetimebox("spinner").timespinner("options").separator;
			var tt = dt[1].split(_2d);
			var _2e = parseInt(tt[0], 10) || 0;
			var _2f = parseInt(tt[1], 10) || 0;
			var _30 = parseInt(tt[2], 10) || 0;
			return new Date(d.getFullYear(), d.getMonth(), d.getDate(),_2e, _2f, _30);
		}
	});
})(jQuery);
